package com.banzhi.shop.activity;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.banzhi.shop.R;
import com.banzhi.shop.adapter.CommonAdapter;
import com.banzhi.shop.base.BaseActivity;
import com.banzhi.shop.entity.RequestWithdraw.TixianList_Data;
import com.banzhi.shop.model.CallBack.WithdrawDepositListCallBack;
import com.banzhi.shop.model.IWithdrawDeposiListModel;
import com.banzhi.shop.model.Impl.WithdrawDepositListImpl;
import com.banzhi.shop.utils.ASUtils;
import com.banzhi.shop.utils.DateUtil;
import com.banzhi.shop.utils.EndlessRecyclerOnScrollListener;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.NoTokenUtils;
import com.banzhi.shop.utils.NumberUtils;
import com.banzhi.shop.utils.ToastUtils;
import com.banzhi.shop.utils.Translucent;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import static com.banzhi.shop.R.color.colorWhite;
import static com.banzhi.shop.R.color.themeColor;
import static com.banzhi.shop.R.id.recyclerview;
import static com.banzhi.shop.R.id.srl_listview;
import static com.banzhi.shop.R.id.tv_date;
import static com.banzhi.shop.R.id.tv_money;

/**
 * 提现记录
 */
public class WithdrawDepositListActivity extends BaseActivity implements View.OnClickListener, SwipeRefreshLayout.OnRefreshListener, WithdrawDepositListCallBack {
    private ImageView ivback;
    private SwipeRefreshLayout swipeRefreshLayout;
    private RecyclerView recyclerView;
    private LinearLayoutManager layoutManager;
    private int page = 1;
    private IWithdrawDeposiListModel model;
    private List<TixianList_Data> list_datas = new ArrayList<>();
    private RelativeLayout rlnodata;
    private CommonAdapter<TixianList_Data> adapter;
    private boolean loading;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_withdraw_deposit_list);
        Translucent.setTranslucent(this);
        setinits();
        setListeners();
        model = new WithdrawDepositListImpl();
        LoadingDialogUtils.showProgress(this, "正在加载,请稍后~");
        model.getData(page, this);
    }

    private void setinits() {
        ivback = (ImageView) findViewById(R.id.iv_back);
        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(srl_listview);
        swipeRefreshLayout.setColorSchemeResources(themeColor);
        swipeRefreshLayout.setProgressBackgroundColorSchemeResource(colorWhite);
        recyclerView = (RecyclerView) findViewById(recyclerview);
        layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);
        rlnodata = (RelativeLayout) findViewById(R.id.rl_nodata);
    }

    private void setListeners() {
        ivback.setOnClickListener(this);
        swipeRefreshLayout.setOnRefreshListener(this);
        recyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener(layoutManager) {
            @Override
            public void onLoadMore() {
                page++;
                LoadingDialogUtils.showProgress(WithdrawDepositListActivity.this, "正在加载,请稍后~");
                model.getData(page, WithdrawDepositListActivity.this);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }

    @Override
    public void onRefresh() {
        page = 1;
        LoadingDialogUtils.showProgress(this, "正在加载,请稍后~");
        model.getData(page, this);
    }

    /**
     * 获取提现记录成功
     *
     * @param object
     */
    @Override
    public void ongetDataSuccess(Object object) {
        if (swipeRefreshLayout.isRefreshing()) {
            swipeRefreshLayout.setRefreshing(false);
        }
        List<TixianList_Data> data = (List<TixianList_Data>) object;
        if (page == 1) {
            list_datas.clear();

        } else {
            if (data.size() == 0) {
//                ToastUtils.makeToastShort("没有更多了");
                return;
            }
        }

        list_datas.addAll(data);

        if (list_datas.size() == 0) {
            rlnodata.setVisibility(View.VISIBLE);
        } else {
            rlnodata.setVisibility(View.GONE);
        }
        if (adapter == null) {
            adapter = new CommonAdapter<TixianList_Data>(this, R.layout.item_tixian_list, list_datas) {
                @Override
                public void convert(ViewHolder holder, TixianList_Data data, int position) {
                    if (ASUtils.isCanUse(data.getCreatetime())) {
                        holder.setText(tv_date, DateUtil.getData(data.getCreatetime()));
                    }
                    if (ASUtils.isCanUse(data.getMoney())) {
                        holder.setText(tv_money, NumberUtils.DecimalFormat(data.getMoney()));
                    }
                }
            };
            recyclerView.setAdapter(adapter);
        } else {
            adapter.notifyDataSetChanged();
        }
    }

    /**
     * 获取提现记录失败
     *
     * @param object
     */
    @Override
    public void ongetDataError(Object object) {
        JSONObject jsonObject = (JSONObject) object;
        NoTokenUtils.loginOut(this, jsonObject);
        ToastUtils.makeToastShort(jsonObject.optString("msg"));
    }
}
